var basePath = "/btCloud/";
var hostName = "http://47.243.44.203:10001"

function get(url, data, success) {
	uni.showLoading();
	uni.request({
		url: hostName + basePath + url + buildParams(data, true),
		header: {
			Authorization: 'Bearer ' + uni.getStorageSync('token')
		},
		method: 'get',
		success: function(res) {
			uni.hideLoading();
			console.log('abpAjax', res)
			if (res.statusCode == '401') {
				uni.showToast({
					title: "接口鉴权失效",
					duration: 5000
				})
				uni.setStorageSync('isLogin', false);
				goLoginPageTimeOut()
				return
			} else if (res.statusCode != '200') {
				uni.showToast({
					title: "接口错误",
					duration: 5000
				})
			} else if (res.data.result.code != "200"||res.data.code !='200') {
				uni.showToast({
					title: res.data.result.message||es.data.message,
					duration: 5000
				})
			} else {
				if (success) {
					success(res.data.result.data)
				}
			}
		},
		fail: function(obj) {
			uni.hideLoading();
			uni.showToast({
				title: "接口错误",
				duration: 5000
			})
			console.log(obj)
		}
	})
}

function post(url, data, success) {
	uni.showLoading();
	uni.request({
		url: hostName + basePath + url,
		header: {
			Authorization: 'Bearer ' + uni.getStorageSync('token')
		},
		method: 'post',
		data: data,
		success: function(res) {
			uni.hideLoading();
			console.log('abpAjax', res)
			if (res.statusCode == '401') {
				uni.showToast({
					title: "接口鉴权失效",
					duration: 5000
				})
				uni.setStorageSync('isLogin', false);
				goLoginPageTimeOut()
				return
			} else if (res.statusCode != '200') {
				uni.showToast({
					icon:'none',
					title: "接口错误",
					duration: 5000
				})
			} else if (res.data.code != 200) {
				uni.showToast({
					icon:'none',
					title: res.data.message,
					duration: 5000
				})
			} else {
				if (success) {
					success(res.data)
				}
			}
		},
		fail: function(obj) {
			uni.hideLoading();
			uni.showToast({
				title: "接口错误",
				duration: 5000
			})
			console.log(obj)
		}
	})
}
function del(url, data, success) {
	uni.showLoading();
	uni.request({
		url: hostName + basePath + url + buildParams(data, true),
		header: {
			Authorization: 'Bearer ' + uni.getStorageSync('token')
		},
		method: 'delete',
		success: function(res) {
			uni.hideLoading();
			console.log('abpAjax', res)
			if (res.statusCode == '401') {
				uni.showToast({
					title: "接口鉴权失效",
					duration: 5000
				})
				uni.setStorageSync('isLogin', false);
				goLoginPageTimeOut()
				return
			} else if (res.statusCode != '200') {
				uni.showToast({
					title: "接口错误",
					duration: 5000
				})
			} else if (res.data.result.code != "200") {
				uni.showToast({
					title: res.data.result.message,
					duration: 5000
				})
			} else {
				if (success) {
					success(res.data.result.data)
				}
			}
		},
		fail: function(obj) {
			uni.hideLoading();
			uni.showToast({
				title: "接口错误",
				duration: 5000
			})
			console.log(obj)
		}
	})
}

function login(data) {
	uni.showLoading();
	uni.request({
		url: hostName + "Home/Authenticate",
		method: 'post',
		data: data,
		success: function(res) {
			uni.hideLoading();
			console.log('abpAjax', res)
			if (res.statusCode == '401') {
				uni.showToast({
					title: "接口鉴权失效",
					duration: 5000
				})
				uni.setStorageSync('isLogin', false);
				goLoginPageTimeOut()
				return
			} else if (res.statusCode != '200') {
				uni.showToast({
					title: "接口错误",
					duration: 5000
				})
			} else if (res.data.result.code != "200") {
				uni.showToast({
					title: res.data.result.message,
					duration: 5000
				})
			} else {
				uni.setStorageSync('token', res.data.result.data.accessToken, res.data.result.data.expireInSeconds)
				uni.setStorageSync('isLogin', true, res.data.result.data.expireInSeconds)
				uni.navigateTo({
					url: "/pages/order"
				})
			}
		},
		fail: function(obj) {
			uni.hideLoading();
			uni.showToast({
				title: "接口错误",
				duration: 5000
			})
			console.log(obj)
		}
	})
}


function goLoginPageTimeOut() {
	var Curr = getCurrentPages()
	if (Curr.length > 0) {
		if (Curr[Curr.length - 1].route != "pages/index") {
			uni.navigateTo({
				url: '/pages/index',
			})
		}
	}
}

function buildParams(data, isPrefix) {
	isPrefix = isPrefix ? isPrefix : false
	let prefix = isPrefix ? '?' : ''
	let _result = []
	for (let key in data) {
		let value = data[key]
		// 去掉为空的参数
		if (['', undefined, null].includes(value)) {
			continue
		}
		if (value.constructor === Array) {
			value.forEach(_value => {
				_result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
			})
		} else {
			_result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
		}
	}
	return _result.length ? prefix + _result.join('&') : ''
}
const api = {
	post,
	get,
	del,
	login
}
export default api
